<?php require_once($_SERVER['DOCUMENT_ROOT']."/defaultconf.mex"); 
require_once($_SERVER['DOCUMENT_ROOT']."/Modules/FTK9D4D0/modconf.mex"); 
MYTCONN(); 
	//==========================Vars===========================		
	$TimeStart = $_POST['TStart'];
	$TimeEnd = $_POST['TEnd'];
	$cb_Banco = 0;
	$Country = null;
	$Origen0 = null;
	$QOrigen0 = null;
	$Origen1 = null;
	$QOrigen1 = null;
	$QCorresponsal = null;
	$Status = null;
	$QStatus = null;
	$IsAlbaran = '';
	$sysFlag = array(1=>'',2=>'Confirmado');
	$sysModo = array(1=>'Enviar',2=>'Recebido');

	$OrderAD1 = 'ASC'; 
	$OrderAD2 = 'ASC'; 
	$OrderAD3 = 'ASC';

	$OrderBy1 = '`tdm_ID` '.$OrderAD1; 
	$OrderBy2 = null; 
	$OrderBy3 = null;

	if(isset($_POST['Country']) && $_POST['Country'] != null) {
	$Country = ' AND `BnfPais` = '.$_POST['Country'];
	}
	if(isset($_POST['cb_Banco']) && $_POST['cb_Banco'] != null) {
	$cb_Banco = $_POST['cb_Banco'];
	}
	if(isset($_POST['tdm_Origen0']) && $_POST['tdm_Origen0'] != null) {
	$Origen0 = $_POST['tdm_Origen0'];
	$QOrigen0 = ' AND `tdm_Domain` IN (SELECT `OriNumero` FROM `origenes` WHERE `OriDominio` = '.$Origen0.')';
	}
	if($_POST['tdm_Corresponsal'] > 0) {
	$QCorresponsal = " AND `tdm_Corresponsal` = ".$_POST['tdm_Corresponsal'];
	}
	if(isset($_POST['tdm_Modo']) && $_POST['tdm_Modo'] != null) {
	$Modo = $_POST['tdm_Modo'];
	$QModo = ' AND `tdm_Modo` = '.$Modo;
	}
	
	if($_POST['OrderBy1'] != null) {
	$OrderBy1 = '`'.$_POST['OrderBy1'].'` '.$_POST['OrderAD1'];
	}
	if($_POST['OrderBy2'] != null) {
	$OrderBy2 = ',`'.$_POST['OrderBy2'].'` '.$_POST['OrderAD2'];
	}
	if($_POST['OrderBy3'] != null) {
	$OrderBy3 = ',`'.$_POST['OrderBy3'].'` '.$_POST['OrderAD3'];
	}
	if(isset($_POST['VIEWColumns'])) { 
		$Colums = $_POST['VIEWColumns'];
		$CCols = count($_POST['VIEWColumns']);
	}
if(isset($Colums)) {

  $QColums = '';
  for ($x = 0; $x < $CCols; $x++) {
		$QColums .= ',`'.$Colums[$x].'`';
  } 
	$cmStr = '';
	$dsStr = '';
    for ($x = 0; $x < $CCols; $x++) {
			$dsStr .= ',{name: \''.$Colums[$x].'\', type:\'string\'}';
	} 

    for ($x = 0; $x < $CCols; $x++) {
			$cmStr .= ',{header: "'.$Colums[$x].'", width: 50, sortable: true, dataIndex: \''.$Colums[$x].'\'}';
	}

	$GET = $GLOBALS['MYT']->db_array("SELECT `tdm_ID`,`tdm_DateValue`,`tdm_Date`, `tdm_TFraSent`,`tdm_InvTotal`,`tdm_TFraPay`,`dom_RazonSocial`,CorrNombre,pai_NombreUSD,cvt_Name,tdm_Currency,tdm_Modo,tdm_Flag
  				FROM `transmitionsdomain`
			INNER JOIN `dominios` ON `dom_ID` = (SELECT `OriDominio` FROM `origenes` WHERE `OriNumero` = tdm_Domain)
			LEFT OUTER JOIN `corresponsales` ON `CorrNumero` = `tdm_Corresponsal`
			LEFT OUTER JOIN hd_paises ON pai_CodElite = tdm_Country
			LEFT OUTER JOIN hd_cashiertypes ON cvt_ID = tdm_Cashier
			WHERE `tdm_Date` BETWEEN '{$TimeStart} 00:00:00' AND '{$TimeEnd} 23:59:59'
			AND tdm_Service = 1 {$QOrigen0}{$QCorresponsal}{$Country}{$QModo}
			ORDER BY {$OrderBy1}{$OrderBy2}{$OrderBy3}");
	$CGET = $GET[0];
 
    $sums['EUR'] = 0;
	$sums['USD'] = 0;
	$sums['BRL'] = 0;
	$rsStr = '';
	
	for ($i=1;$i<=$CGET;$i++) { 
        $sums['EUR'] = $sums['EUR']+$GET[$i]['tdm_TFraSent'];
		$sums['USD'] = $sums['USD']+$GET[$i]['tdm_TFraPay'];
		$sums['BRL'] = $sums['BRL']+$GET[$i]['tdm_InvTotal'];
   $rsStr .= '['.$i.','.$GET[$i]['tdm_ID'].',\''.$GET[$i]['tdm_DateValue'].'\',\''.$GET[$i]['tdm_Date'].'\',\''.number_format($GET[$i]['tdm_TFraSent'],2,'.','').'\',\''.$_SESSION[$SESSST.'CURS'][$GET[$i]['tdm_Currency']]['CurLabel'].'\',\''.number_format($GET[$i]['tdm_TFraPay'],2,'.','').'\',\'\',\''.$GET[$i]['tdm_InvTotal'].'\',\''.$GET[$i]['dom_RazonSocial'].'\',\''.$sysFlag[$GET[$i]['tdm_Flag']].'\',\''.$GET[$i]['CorrNombre'].'\',\''.$GET[$i]['pai_NombreUSD'].'\',\''.$GET[$i]['cvt_Name'].'\',\''.$GET[$i]['tdm_Modo'].'\'],';
    } ?>
<script charset="utf-8" type="text/javascript">
disableSelection(document.getElementById("TDMResponse"))

Ext.onReady(function(){
 Ext.QuickTips.init();

    var myData = [<?php echo substr($rsStr,0,strlen($rsStr)-1);?>];

    var ds = new Ext.data.Store({
        reader: new Ext.data.ArrayReader({}, [
		   {name: 'id'},
		   {name: 'tdm_ID', type: 'int'},
		   {name: 'DataVal', type: 'date', dateFormat: 'Y-m-d H:i:s'},
		   {name: 'Data', type: 'date', dateFormat: 'Y-m-d H:i:s'},
		   {name: 'mon_1', type: 'float'},
		   {name: 'mon_1cur', type: 'string'},
		   {name: 'mon_2', type: 'float'},
		   {name: 'mon_2cur', type: 'string'},
		   {name: 'mon_3', type: 'float'},
		   {name: 'domain', type: 'string'},
		   {name: 'tdmFlag', type: 'string'},
		   {name: 'opt1', type: 'string'},
		   {name: 'opt2', type: 'string'},
		   {name: 'opt3', type: 'string'},
		   {name: 'tdmModo', type: 'string'}
        ])
    });
    ds.loadData(myData);
    var colModel = new Ext.grid.ColumnModel([
        {header: "Numero", width: 50, sortable: true, locked:true, dataIndex: 'tdm_ID'},
        {header: "Data de Valor", width: 120, sortable: true, renderer: Ext.util.Format.dateRenderer('Y-m-d H:i:s'), dataIndex: 'DataVal'},
        {header: "Data", width: 120, sortable: true, renderer: Ext.util.Format.dateRenderer('Y-m-d H:i:s'), dataIndex: 'Data'},
        {header: "Enviado", width: 75, sortable: true, locked:true, align: 'right', dataIndex: 'mon_1'},
        {header: "Moeda", width: 30, sortable: true, locked:true, align: 'right', dataIndex: 'mon_1cur'},
        {header: "A Pagar", width: 75, sortable: true, locked:true, align: 'right',dataIndex: 'mon_2'},
        {header: "Moeda", width: 30, sortable: true, locked:true, align: 'right',dataIndex: 'mon_2cur'},
        {header: "Remessas", width: 60, sortable: true, locked:true, align: 'right',dataIndex: 'mon_3'},
        {header: "Loja", width: 250, sortable: true, locked:true,dataIndex: 'domain'},
        {header: "Estado", width: 80, sortable: true, locked:true,dataIndex: 'tdmFlag'},
        {header: "Correspondente", width: 120, sortable: true, locked:true,dataIndex: 'opt1'},
        {header: "Pais", width: 120, sortable: true, locked:true,dataIndex: 'opt2'},
        {header: "Caixa", width: 120, sortable: true, locked:true,dataIndex: 'opt3'},
        {header: "Modo", width: 50, sortable: true, locked:true,dataIndex: 'tdmModo'}
    ]);
	Bars = new Ext.Toolbar({ 
				
		store: this.store, 
		items : [{
			text: "<?php echo $CGET;?> TDMS | EUR: <?php echo number_format($sums['EUR'],2);?> | USD: <?php echo number_format($sums['USD'],2);?> | BRL: <?php echo number_format($sums['BRL'],2);?>"
		},{
		type:'button',
		text:'Confirmar, imprimir e comunicar recebimento',
                handler:function(){
			var ss = ''; 
			var m = selmode.getSelections();				
			for(var i = 0, len = m.length; i < len; i++){
				ss += m[i].get("tdm_ID") + ",";}
				document.getElementById('tdm_selected').value=ss;
				document.TDMFilters.action='_response/rsproxy.php?pr=fn_tdmvalidate';
				subform('TDMFilters','rsTdmValidate');
			}
		},{
		text:'Reimprimir',
                handler:function(){
			var ss = ''; 
			var m = selmode.getSelections();				
			for(var i = 0, len = m.length; i < len; i++){
				ss += m[i].get("tdm_ID") + ",";}
				document.getElementById('tdm_selected').value=ss;
				document.TDMFilters.action='_response/rsproxy.php?pr=fn_tdmprint';
				subform('TDMFilters','rsTdmValidate');
			}
		},
		"Enviado <input type=text name=sum1 id=sum1 value=0 readonly />",
		"A pagar <input type=text name=sum2 id=sum2 value=0 readonly />",
		"<div id=rsTdmValidate style=float:right></div>"]
	});

   var selmode = new Ext.grid.RowSelectionModel({
	listeners: {
		rowselect: function(sm, row, rec) {
			var sum1 = 0;
			var sum2 = 0;
			var m = sm.getSelections();
				for(var i = 0, len = m.length; i < len; i++){
				sum1 += m[i].get("mon_1");
				sum2 += m[i].get("mon_2");
				
			}
				document.getElementById('sum1').value=sum1.toFixed(2);
				document.getElementById('sum2').value=sum2.toFixed(2);
	          },
		  buffer:50
	}});
   new Ext.grid.GridPanel({
       height: document.documentElement.clientHeight-300,
        ds: ds,
	    cm: colModel,
	    sm: selmode,
	    bbar: Bars,
        renderTo: 'TDMResponse'
    });
		
});
</script>
<?php } ?>